package org.tc.feign.storefront;

import org.gdtc.storefront.entity.Saleafter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tc.feign.service.FeignProblemService;
import org.tc.feign.service.FeignProductService;
import org.tc.feign.service.FeignSaleaService;
import java.util.List;

@RequestMapping("/Salea")
@RestController
@CrossOrigin(origins = {"*"}, methods = {RequestMethod.GET, RequestMethod.POST}, allowCredentials = "true")
public class FeignSaleaController {
    @Autowired
    private FeignSaleaService feignSaleaService;
    @Autowired
    private FeignProblemService feignProblemService;
    @Autowired
    private FeignProductService feignProductService;

    /**
     * 查询所有的售后
     * 垮库跨服务
     * @param storeid
     * @return
     */
    @RequestMapping("/QueryAllSalea")
    @ResponseBody
    public List<Saleafter> QueryAllSalea(Integer storeid)
    {
        List<Saleafter> list=feignSaleaService.QueryAllSalea(storeid);
        return list;
    }


    /**售后
     * 如果退款将商品添加到问题商品仓库
     * 退款状态必须是 state=1的情况下添加到问题商品仓库
     * 暂缺退款接口！！！！！
     * @param id 售后ID 根据ID修改状态
     * @param storeid 分店ID
     * @param state 状态 0未处理 1退款 2不处理 不退款
     * @return
     */
    @RequestMapping("/UpdSaleaState")
    @ResponseBody                   //售后ID      分店ID         售后状态
    public Object UpdSaleaState(Integer id,Integer storeid,Integer state)
    {
        //修改状态                                售后ID 分店ID  状态
        int updstate=feignSaleaService.UpdSaleaState(id, storeid,state);
        //如果状态为1的情况下添加商品到问题仓库
        if (state==1)
        {      //商品ID                                售后ID
            int commid=feignSaleaService.QuerycommodityId(id);
            //先查出来和这个商品相关的数据                        分店ID   售后ID
            List<Saleafter> list=feignSaleaService.SelectUpdSalea(storeid,id);
            //循环遍历出来
            for (Saleafter attribute:list)
            {
                System.out.println("list的长度："+list.size());
                /*for (int i=0;i<list.size();i++)
                {*/
                    int commodityid=attribute.getCommodityid();
                    int saleafterid=attribute.getId();
                    int stock=attribute.getNumber();//获取问题商品个数
                    int orderid=attribute.getStoreid();
                    //查询问题商品个数                                商品ID  分店ID
                    int productcount=feignProductService.QuerytoSalea(commid,storeid);
                    System.out.println("商品个数"+productcount);
                    System.out.println("问题商品个数"+stock);
                    int count=productcount-stock;
                    System.out.println("商品个数剩余"+count);
                    //修改商品个数
                    int updcount=feignProductService.UpdtoSalea(count,commid,storeid);
                    String because=attribute.getBecause();
                    System.out.println(commodityid+"\t"+saleafterid+"\t"+stock+"\t"+storeid+"\t"+because+"\t"+orderid);
                    //获取进库时间
                    java.util.Calendar c=java.util.Calendar.getInstance();
                    java.text.SimpleDateFormat f=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String jointime=f.format(c.getTime());
                    System.out.println(jointime);
                    //把循环遍历出来的数据放进问题商品仓库      分店ID   商品ID      售后ID   商品个数   进库时间  原因
                   int procount=feignProblemService.InsProblem(storeid,commodityid,saleafterid,stock,jointime,because,orderid);
                return procount;
            }
            }
        return updstate;
    }

    /**
     * 根据状态筛选售后
     * @param storeid 分店ID
     * @param state 状态
     * @return
     */
    @RequestMapping("/QuerySaleaWhere")
    @ResponseBody
    public List<Saleafter> QuerySaleaWhere(Integer storeid,Integer state)
    {
        List<Saleafter> list=feignSaleaService.QuerySaleaWhere(storeid, state);
        return list;
    }

    /**
     * 默认加载所有
     *
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Saleafter> selectAll() {
        List<Saleafter> list = feignSaleaService.selectAll();
        return list;
    }
}
